package com.cheyun.netsalev3.viewmodel

import android.content.Intent
import android.view.View
import androidx.databinding.ObservableField
import androidx.work.Data
import androidx.work.OneTimeWorkRequestBuilder
import androidx.work.WorkManager
import com.cheyun.netsalev3.bean.dm.DmSearch
import com.cheyun.netsalev3.repository.DmSearchRepository
import com.cheyun.netsalev3.view.dm.DmSearchResultActivity
import com.cheyun.netsalev3.workers.DmSearchDatabaseWorker

/**
 * @author Michael
 * @date  2018/12/21 - 10:07.
 */
class DmSearchActivityViewModel(var searchRepository: DmSearchRepository) : BaseViewModel() {

    var keyword = ObservableField<String>("")
    var planType = ObservableField<Int>(0);
    var status = ObservableField<Int>(0);
    fun search(view: View) {
        if (keyword.get().isNullOrBlank()) {

        } else {
            val myData = Data.Builder()
                    .putString("key", keyword.get().toString())
                    .build()
            val compressionWork = OneTimeWorkRequestBuilder<DmSearchDatabaseWorker>()
                    .setInputData(myData).build()
            WorkManager.getInstance().enqueue(compressionWork)
            startData(view, keyword.get().toString())
        }
    }

    /**
     * 开始搜索
     */
    fun startData(view: View, key: String) {
        var intent = Intent(view.context, DmSearchResultActivity::class.java)
        intent.putExtra("searchKey", key)
//        intent.putExtra("planType", planType.get()?.plus(1))
        intent.putExtra("status", status.get()?.plus(1))
        view.context.startActivity(intent)
    }

    /**
     * item点击
     */
    fun searchItem(view: View, item: DmSearch) {
        startData(view, item.key)
    }

//    var searchlist = searchRepository.getDmlist()
    var searchlist = searchRepository.getlist()

    /**
     * d删除
     */
    fun deleteTabe(view: View) {
//        searchRepository.deleteDmTabe()
        searchRepository.deleteTabe()
    }
}